package com.thinkingstar.iads.projectcenter.serviceImpl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import org.nutz.lang.util.NutMap;

import com.thinkingstar.iads.common.jsonUtils.AjaxResult;
import com.thinkingstar.iads.datacenter.entity.common.dao.CommonDao;

import io.swagger.models.auth.In;

/**
 * @ClassName: ExcelPoiManagerServiceImpl 
 * @Description: excel操作相关维护
 * @author 1157372219@qq.com
 * @date  
 */
@IocBean
public class ExcelPoiManagerServiceImpl {
	
	@Inject
	private CommonDao commonDao;
	
	//提交更新
	public AjaxResult updateData(List <Map <Integer , Object>> data_list , List <NutMap> field_list , String table_name , String project_id){
		tidyData(data_list,field_list);
		//存储表头
		Map <Integer , Object> head_map = data_list.get(0);
		List <NutMap> _nut_map_list = new ArrayList<>();
		//编辑数据集
		for(int i=1;i<data_list.size();i++){
			NutMap _nut_map = new NutMap();
			Map <Integer , Object> data = data_list.get(i);
			Set<Entry<Integer, Object>> set = data.entrySet();
			for(Entry<Integer, Object> entry : set){
				Integer index = entry.getKey();
				_nut_map.put(String.valueOf(head_map.get(index)), entry.getValue());
			}
			_nut_map_list.add(_nut_map);
		}
		//清空历史记录
		commonDao.delRecord(table_name, project_id);
		commonDao.addData(table_name, _nut_map_list, project_id);
		return AjaxResult.success("上传成功!");
	}
	
	//数据整理
	public void tidyData(List <Map <Integer , Object>> data_list , List <NutMap> field_list){
		Map <Integer , Object> head_map = data_list.get(0);
		Set<Entry<Integer, Object>> set = head_map.entrySet();
		for(Entry<Integer, Object> entry : set){
			Integer index = entry.getKey();
			Object value = entry.getValue();
			for(NutMap nutMap : field_list){
				String field_name = nutMap.getString("field_name");
				String field_comment = nutMap.getString("field_comment");
				if(value.equals(field_comment)){
					entry.setValue(field_name);
				}
			}
		}
		
	}
	


}
